package cn.stylefeng.guns.modular.gridsystem.controller;

import cn.stylefeng.guns.core.annotion.BusinessLog;
import cn.stylefeng.guns.core.enums.LogAnnotionOpTypeEnum;
import cn.stylefeng.guns.core.pojo.response.ErrorResponseData;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.core.pojo.response.SuccessResponseData;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import cn.stylefeng.guns.modular.gridsystem.service.PipConfigService;
import cn.stylefeng.guns.modular.gridsystem.model.param.PipConfigParam;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * 管道配置 控制器
 *
 * @author: ssy
 * @date: 2021/11/22 11:57
 */
@RestController
public class PipConfigController {

    @Resource
    private PipConfigService pipConfigService;

    /**
     * 分页查询管道配置
     *
     * @author ssy
     * @date 2021/11/22 11:57
     */
    @GetMapping("/pipConfig/page")
    @BusinessLog(title = "管道配置_分页查询", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData page(PipConfigParam pipConfigParam) {
        return new SuccessResponseData(pipConfigService.page(pipConfigParam));
    }

    /**
     * 获取全部管道配置
     *
     * @author ssy
     * @date 2021/11/22 11:57
     */
    @GetMapping("/pipConfig/list")
    @BusinessLog(title = "管道配置_查询所有", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData list( ) {
        return new SuccessResponseData(pipConfigService.list());
    }

    /**
     * 查看详情管道配置
     *
     * @author ssy
     * @date 2021/11/22 11:57
     */
    @GetMapping("/pipConfig/detail")
    @BusinessLog(title = "管道配置_查看详情", opType = LogAnnotionOpTypeEnum.DETAIL)
    public ResponseData detail(@Validated(PipConfigParam.detail.class) PipConfigParam pipConfigParam) {
        return new SuccessResponseData(pipConfigService.detail(pipConfigParam));
    }

    /**
     * 添加管道配置
     *
     * @author ssy
     * @date 2021/11/22 11:57
     */
    @PostMapping("/pipConfig/add")
    @BusinessLog(title = "管道配置_增加", opType = LogAnnotionOpTypeEnum.ADD)
    public ResponseData add(@RequestBody @Validated(PipConfigParam.add.class) PipConfigParam pipConfigParam) {
        String code = pipConfigParam.getCode();
        int count = pipConfigService.countByCode(code);
        if(count>0){
            return new ErrorResponseData(500,"该编码已存在");
        }
        pipConfigService.add(pipConfigParam);
        return new SuccessResponseData();
    }

    /**
     * 删除管道配置
     *
     * @author ssy
     * @date 2021/11/22 11:57
     */
    @PostMapping("/pipConfig/delete")
    @BusinessLog(title = "管道配置_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    public ResponseData delete(@RequestBody @Validated(PipConfigParam.delete.class) PipConfigParam pipConfigParam) {
        if(pipConfigParam.getStatus() == 1){
            return new ErrorResponseData(500,"该配置正在使用中，无法删除");
        }
        pipConfigService.delete(pipConfigParam);
        return new SuccessResponseData();
    }

    /**
     * 编辑管道配置
     *
     * @author ssy
     * @date 2021/11/22 11:57
     */
    @PostMapping("/pipConfig/edit")
    @BusinessLog(title = "管道配置_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public ResponseData edit(@RequestBody @Validated(PipConfigParam.edit.class) PipConfigParam pipConfigParam) {
        pipConfigService.edit(pipConfigParam);
        return new SuccessResponseData();
    }

    /**
     * 修改状态
     *
     * @author ssy
     * @date 2021/11/22 11:57
     */
    @PostMapping("/pipConfig/changeStatus")
    @BusinessLog(title = "管道配置_修改状态", opType = LogAnnotionOpTypeEnum.CHANGE_STATUS)
    public ResponseData changeStatus(@RequestBody @Validated(PipConfigParam.changeStatus.class) PipConfigParam pipConfigParam) {
        pipConfigService.changeStatus(pipConfigParam);
        return new SuccessResponseData();
    }

}
